import { NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { getUser } from '@/lib/authUtils'
import { VmsUser } from '@/types/Model'
export const dynamic = 'force-dynamic' 

export async function GET(request: Request): Promise<Response> {
  try {
    const userid = await getUser(request)
    if (!userid) {
      return NextResponse.json(
        { error: '未授权或登录信息过期，请登录' },
        { status: 401 }
      )
    }
    if (userid instanceof NextResponse) {
      return userid
    }

    // Get user from database
    const user = await prisma.vms_user.findUnique({
      where: { id: userid },
      select: {
        id: true,
        email: true,
        status: true,
        roles: true,
        ai_count: true,
        copy_count: true,
        script_count: true,
        topic_count: true,
        voice_count: true,
        subtitle_count: true,
        ai_count_limit: true,
        copy_count_limit: true,
        script_count_limit: true,
        topic_count_limit: true,
        voice_count_limit: true,
        subtitle_count_limit: true,
        vip_type: true,
        vip_end_time: true,
        zcsj: true
      }
    })

    if (!user) {
      return NextResponse.json(
        { error: 'User not found' },
        { status: 404 }
      )
    }

    // Return user data without sensitive information
    return NextResponse.json(user, { status: 200 })

  } catch (error) {
    console.error('Error fetching user:', error)
    return NextResponse.json(
      { error: 'Unauthorized' },
      { status: 401 }
    )
  }
}
